package com.atguigu.flink.wordcount;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import static org.apache.flink.api.common.typeinfo.Types.*;


public class Demo9_WEBUI
{
    public static void main(String[] args) throws Exception {

        //放flink的参数
        //WEBUI: 运行web应用，绑定端口，用于访问
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",3333);

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(1);

        DataStreamSource<String> dataStreamSource = env.socketTextStream("hadoop103",8888);

         dataStreamSource.flatMap( (String inputLine, Collector<Tuple2<String,Integer>> collector) -> {
            String[] words = inputLine.split(" ");

            for (String word : words) {
                collector.collect(Tuple2.of(word, 1));
            }
        })
           .returns(TUPLE(STRING,INT))
         .keyBy( ele -> ele.f0)
         .sum(1)
         .print();


        env.execute();

    }
}
